<?php

namespace app\store\controller\appointment;

use app\store\controller\Controller;
use app\store\model\appointment\Time as TimeModel;
use app\store\model\appointment\Type as TypeModel;

/**
 * 预约时间
 * Class Balance
 * @package app\store\controller\appointment
 */
class Time extends Controller
{
    /**
     * 会员等级列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new TimeModel;
        $list = $model->getListByPage();

        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new TimeModel;
        if (!$this->request->isAjax()) {
            $typeModel = new TypeModel();
            $type_list = $typeModel->getList();

            return $this->fetch('add', compact('type_list'));
        }
        // 新增记录
        if ($model->add($this->postData('time'))) {
            return $this->renderSuccess('添加成功', url('appointment.time/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     * @param $time_id
     * @return array|bool|mixed
     */
    public function edit($time_id)
    {
        // 会员等级详情
        $model = TimeModel::detail($time_id);
        if (!$this->request->isAjax()) {
            $typeModel = new TypeModel();
            $type_list = $typeModel->getList();

            return $this->fetch('edit', compact('model', 'type_list'));
        }
        // 新增记录
        if ($model->edit($this->postData('time'))) {
            return $this->renderSuccess('更新成功', url('appointment.time/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 软删除
     * @param $time_id
     * @return array|bool
     */
    public function delete($time_id)
    {
        // 会员等级详情
        $model = TimeModel::detail($time_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}